package com.mdd.admin.controller;

import com.mdd.common.entity.student.Student;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class FontConversionUtil {

    // https://blog.csdn.net/weixin_46426270/article/details/137041441

    //字宽
    private int width;
    //字高
    private int height;
    //字数量
    private int count;
    //字体路径
    private String fontPath;
    //待取模字符串
    private String target;
    //结果
    private byte[] result;

    /**
     * 构造方法
     * @return
     */
    public FontConversionUtil(int width, int height, String fontPath, String target,int count) {
        this.width = width;
        this.height = height;
        this.fontPath = fontPath;
        this.target = target;
        this.count = count;

    }

    // 1为亮
    public byte[] Conversion(){
        BufferedImage image = new BufferedImage(width*count, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        try {
            // 加载字体文件
            URL resource = Student.class.getClassLoader().getResource(fontPath);
            String path = resource.getPath();

            File fontFile = new File(path);
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);

            // 设置字体样式和大小
            font = font.deriveFont(Font.PLAIN, width);
            g2d.setFont(font);
            g2d.setColor(Color.BLACK);
            // 第三个参数调整字体上下偏移
            g2d.drawString(target, 0, width*6/7);
            g2d.dispose();
        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }
        byte c = 0x00;
        result = new byte[height*width*count/8];
        //位运算
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width*count/8; x++) {
                c = (byte) 0xff;
                for (int i = 0; i < 8; i++) {
                    c = (byte) (c<<1);
                    if(image.getRGB(x*8+i, y) == Color.BLACK.getRGB()){
                        c = (byte) (c | 0x1);
                    }
                }
                result[x+y*width*count/8]=c;
            }
        }
        return result;
    }

    // 0为亮
    public byte[] Conversion2(){
        BufferedImage image = new BufferedImage(width*count, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        try {
            // 加载字体文件
            URL resource = Student.class.getClassLoader().getResource(fontPath);
            String path = resource.getPath();

            File fontFile = new File(path);
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);

            // 设置字体样式和大小
            font = font.deriveFont(Font.PLAIN, width);
            g2d.setFont(font);
            g2d.setColor(Color.BLACK);
            // 第三个参数调整字体上下偏移
            g2d.drawString(target, 0, width*6/7);
            g2d.dispose();
        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }
        byte c = 0x00;
        result = new byte[height*width*count/8];
        //位运算
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width*count/8; x++) {
                c = (byte) 0x00;
                for (int i = 0; i < 8; i++) {
                    c = (byte) (c<<1);
                    if(image.getRGB(x*8+i, y) != Color.BLACK.getRGB()){
                        c = (byte) (c | 0x01);
                    }
                }
                result[x+y*width*count/8]=c;
            }
        }
        return result;
    }

    /**
     * 打印16进制
     * @return
     */
    public void print16(){
        StringBuilder stringBuilder = new StringBuilder();
        int j = 0;
        for (byte b : result) {
            stringBuilder.append(String.format("0x%02X", b)).append(",");
            j++;
            if (j == width*count/8) {
                stringBuilder.append("\n");
                j = 0;
            }
        }
        System.out.println(stringBuilder);
    }
    /**
     * 打印位图
     * @return
     */
    public void printBitMap(){
        int j = 0;
        for (byte b:result){
            for (int i = 7; i >= 0; i--) {
                int bit = (b >> i) & 1; // 右移并且按位与操作
//                System.out.print(bit);
                if (bit == 0) {
                    System.out.print("□ ");
                } else {
                    System.out.print("■ ");
                }
            }
            j++;
            if(j==width*count/8){
                System.out.println();
                j=0;
            }
        }
    }


    public static void main(String[] args) {

        FontConversionUtil fontConversionUtil = new FontConversionUtil(
                16, 16, "simsun.ttc", "我",1);
        byte[] arr = fontConversionUtil.Conversion();
        fontConversionUtil.printBitMap();
        fontConversionUtil.print16();

    }

}
